/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fichiersimulation;

import java.awt.Color;
import java.awt.event.FocusEvent;
import javax.swing.plaf.FileChooserUI;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import java.io.*;
import java.util.regex.*;
import java.util.*;


/**
 *
 * @author Antoine
 */
public class IHMFichierSimulation extends javax.swing.JFrame {

    /**
     * Creates new form IHMFichierSimulation
     */
    public IHMFichierSimulation() {
        initComponents();
    }

    JFileChooser fc = new JFileChooser();
    Pattern p;
    Matcher m;
    
    String nouvelleLigne = System.getProperty("line.separator");
    
    
    
       
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        boutonGenerer = new javax.swing.JButton();
        labelTitre = new javax.swing.JLabel();
        texteEmplacement = new javax.swing.JTextField();
        boutonParcourir = new javax.swing.JButton();
        labelConsignes = new javax.swing.JLabel();
        labelHeureDebut = new javax.swing.JLabel();
        labelHeureFin = new javax.swing.JLabel();
        texteDebutHeure = new javax.swing.JTextField();
        texteFinHeure = new javax.swing.JTextField();
        labelLimitesEtages = new javax.swing.JLabel();
        texteMoyennePersonnes = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        texteLimiteEtageDebut = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        texteLimiteEtageFin = new javax.swing.JTextField();
        labelWeekEnd = new javax.swing.JLabel();
        checkboxWeekEnd = new javax.swing.JCheckBox();
        labelInfo = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Fichier Simulation");

        boutonGenerer.setText("Générer");
        boutonGenerer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonGenererActionPerformed(evt);
            }
        });

        labelTitre.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        labelTitre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelTitre.setText("Outil de génération de fichier de simulation");
        labelTitre.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        texteEmplacement.setForeground(new java.awt.Color(153, 153, 153));
        texteEmplacement.setText("Emplacement du fichier");
        texteEmplacement.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                texteEmplacementFocusLost(evt);
            }
        });

        boutonParcourir.setText("Parcourir");
        boutonParcourir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                boutonParcourirActionPerformed(evt);
            }
        });

        labelConsignes.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        labelConsignes.setText("Personaliser :");

        labelHeureDebut.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelHeureDebut.setText("Heure de début :");

        labelHeureFin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelHeureFin.setText("Heure de fin :");

        texteDebutHeure.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        texteDebutHeure.setText("00:00:01");
        texteDebutHeure.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                texteDebutHeureFocusLost(evt);
            }
        });

        texteFinHeure.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        texteFinHeure.setText("23:59:59");
        texteFinHeure.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                texteFinHeureFocusLost(evt);
            }
        });

        labelLimitesEtages.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelLimitesEtages.setText("Limiter la simulation de l'étage :");

        texteMoyennePersonnes.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        texteMoyennePersonnes.setText("0");
        texteMoyennePersonnes.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                texteMoyennePersonnesFocusLost(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("<html>Nombre de personnes en moyenne <br>par ascenseur (15 max, 0 pour aléatoire) :</html>");

        texteLimiteEtageDebut.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        texteLimiteEtageDebut.setText("-4");
        texteLimiteEtageDebut.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                texteLimiteEtageDebutFocusLost(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel1.setText("à");

        texteLimiteEtageFin.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        texteLimiteEtageFin.setText("40");
        texteLimiteEtageFin.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                texteLimiteEtageFinFocusLost(evt);
            }
        });

        labelWeekEnd.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelWeekEnd.setText("Simuler en week end :");

        checkboxWeekEnd.setMargin(new java.awt.Insets(0, 0, 0, 0));

        labelInfo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        labelInfo.setText("<html>Cet outil génère aléatoirement des trajets d'ascenseurs.<br>La moyenne de trajet d'un jour en semaine est de 1050 et en week end de 450.<br>Le fait de diminuer l'amplitude horraire ou des l'amplitude des étages réduit le nombre moyen de trajets.</html>");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTitre, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 581, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(texteEmplacement)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boutonParcourir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(boutonGenerer))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelConsignes)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelHeureDebut)
                                    .addComponent(labelHeureFin)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(labelLimitesEtages)
                                    .addComponent(labelWeekEnd))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(texteLimiteEtageDebut, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(texteLimiteEtageFin, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(texteFinHeure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(texteDebutHeure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(texteMoyennePersonnes, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(checkboxWeekEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelTitre, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(8, 8, 8)
                .addComponent(labelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(texteDebutHeure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(texteFinHeure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(texteMoyennePersonnes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelConsignes)
                        .addGap(18, 18, 18)
                        .addComponent(labelHeureDebut)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(labelHeureFin)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelLimitesEtages)
                    .addComponent(texteLimiteEtageDebut, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(texteLimiteEtageFin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(checkboxWeekEnd)
                    .addComponent(labelWeekEnd))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(boutonGenerer)
                    .addComponent(texteEmplacement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(boutonParcourir))
                .addContainerGap())
        );

        labelLimitesEtages.getAccessibleContext().setAccessibleName("<HTML>1ère ligne<BR>2ème ligne<BR>3ème ligne</HTML>\" ");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void boutonParcourirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonParcourirActionPerformed
       
       fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        
       int returnVal = fc.showOpenDialog(IHMFichierSimulation.this);
       
       if (returnVal == JFileChooser.APPROVE_OPTION) {
            File returnFile = fc.getSelectedFile();
            texteEmplacement.setText(returnFile.getAbsolutePath());
            texteEmplacement.setForeground(Color.black);
            //This is where a real application would open the file.
        } else {
            System.out.println("Erreur lors de l'obtention du fichier");
        }

    }//GEN-LAST:event_boutonParcourirActionPerformed

    private void texteEmplacementFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_texteEmplacementFocusLost
        if (texteEmplacement.getText().equals(""))
        {
            texteEmplacement.setForeground(new java.awt.Color(153, 153, 153));
            texteEmplacement.setText("Emplacement du fichier");
        }
        
    }//GEN-LAST:event_texteEmplacementFocusLost

    private void texteMoyennePersonnesFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_texteMoyennePersonnesFocusLost
        
        p = Pattern.compile("(^[0-9]|^1[0-5])+$");
        
        m = p.matcher(texteMoyennePersonnes.getText());
        
        if (!m.find())
        {
            texteMoyennePersonnes.setText("0");
        }
    }//GEN-LAST:event_texteMoyennePersonnesFocusLost

    private void texteLimiteEtageDebutFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_texteLimiteEtageDebutFocusLost
        p = Pattern.compile("(^-[0-4]|^[0-9]|^[1-3][0-9])+$");
        
        m = p.matcher(texteLimiteEtageDebut.getText());
        
        if (!m.find())
        {
            texteLimiteEtageDebut.setText("-4");
        }
        
        if (Integer.valueOf(texteLimiteEtageDebut.getText())>= Integer.valueOf(texteLimiteEtageFin.getText()))
        {
            texteLimiteEtageFin.setText(String.valueOf(Integer.valueOf(texteLimiteEtageDebut.getText())+1));
        }
    }//GEN-LAST:event_texteLimiteEtageDebutFocusLost

    private void texteLimiteEtageFinFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_texteLimiteEtageFinFocusLost
        p = Pattern.compile("(^-[0-3]|^[0-9]|^[1-3][0-9]|40)+$");
        
        m = p.matcher(texteLimiteEtageFin.getText());
        
        if (!m.find())
        {
            texteLimiteEtageFin.setText("40");
        }
        
        if (Integer.valueOf(texteLimiteEtageFin.getText()) <= Integer.valueOf(texteLimiteEtageDebut.getText()))
        {
            texteLimiteEtageDebut.setText(String.valueOf(Integer.valueOf(texteLimiteEtageFin.getText())-1));
        }
    }//GEN-LAST:event_texteLimiteEtageFinFocusLost

    private void texteDebutHeureFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_texteDebutHeureFocusLost
        p = Pattern.compile("((^[0-1][0-9]|^2[0-3])(:[0-5][0-9]){2})+$");
        
        m = p.matcher(texteDebutHeure.getText());
        
        if (!m.find())
        {
            texteDebutHeure.setText("00:00:01");
        }
        
        if (texteDebutHeure.getText().compareTo(texteFinHeure.getText()) > 0)
        {
            texteDebutHeure.setText("00:00:01");
        }
        
    }//GEN-LAST:event_texteDebutHeureFocusLost

    private void texteFinHeureFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_texteFinHeureFocusLost
        p = Pattern.compile("((^[0-1][0-9]|^2[0-3])(:[0-5][0-9]){2})+$");
        
        m = p.matcher(texteFinHeure.getText());
        
        if (!m.find())
        {
            texteFinHeure.setText("23:59:59");
        }
        
        if (texteFinHeure.getText().compareTo(texteDebutHeure.getText()) < 0)
        {
            texteFinHeure.setText("23:59:59");
        }
    }//GEN-LAST:event_texteFinHeureFocusLost

    
    
    private void boutonGenererActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_boutonGenererActionPerformed
            
            
            Random rd = new Random();
            int i;
            int boucle;
            int maxVal, minVal;
            String[] tabDebut, tabFin;
            
            int maxHeure=0, maxMinute=0, maxSeconde=0, minHeure=0, minMinute=0, minSeconde=0;
            int cptMinuitHuit = 0, cptVingtMinuit = 0, cptJour=0, cptSoirWK=0;
            int etageDebut=-4, etageFin=0, nbPersonnes, heure, minute, seconde;

            List<TrajetVO> trajets = new ArrayList<>();
            
            tabDebut = texteDebutHeure.getText().split(":");
            tabFin = texteFinHeure.getText().split(":");

            minHeure=Integer.valueOf(tabDebut[0]);
            minMinute=Integer.valueOf(tabDebut[1]);
            minSeconde=Integer.valueOf(tabDebut[2]);
            maxHeure=Integer.valueOf(tabFin[0]);
            maxMinute=Integer.valueOf(tabFin[1]);
            maxSeconde=Integer.valueOf(tabFin[2]);
            
            if (checkboxWeekEnd.isSelected())
            {
                maxVal = 500;
                minVal = 400;
            }
            else
            {
                maxVal = 1100;
                minVal = 1000;
            }
            
            /* Nombre d'itérations de la boucle pour créer les trajets*/
            boucle=rd.nextInt(maxVal-minVal)+minVal;
           
            
            /*Les listes boucle contienent le nombre d'itération de la boucle, que l'on va mélanger pour de l'aléatoire*/
            List<Integer> listBoucleSoirWK = new ArrayList<>();
            List<Integer> listBoucleJour = new ArrayList<>();
            
             
            /*La liste poids défini les pourcentages de traffic en fonction des tranches d'etages */
            List<Integer> listPoids = new ArrayList<>();
            
             if (checkboxWeekEnd.isSelected())
                {
                    /*Traffic en week end*/
                    listPoids.add((boucle*25)/100);
                    listPoids.add((boucle*25)/100 + listPoids.get(0));
                    listPoids.add((boucle*5)/100 + listPoids.get(1));
                    listPoids.add((boucle*0)/100 + listPoids.get(2));
                    listPoids.add((boucle*45)/100 + listPoids.get(3)); 
                    
                    for (i=0;i<=boucle;i++)
                    {
                        listBoucleSoirWK.add(i);
                    }
                     Collections.shuffle(listBoucleSoirWK);
                }
             else
             {
                    /*Traffic en soiree*/
                    listPoids.add((150*25)/100);
                    listPoids.add((150*25)/100 + listPoids.get(0));
                    listPoids.add((150*5)/100 + listPoids.get(1));
                    listPoids.add((150*0)/100 + listPoids.get(2));
                    listPoids.add((150*45)/100 + listPoids.get(3));
                    
                    for (i=0;i<=150;i++)
                    {
                        listBoucleSoirWK.add(i);
                    }
                     Collections.shuffle(listBoucleSoirWK);

                     /*Traffic en journee*/
                    listPoids.add(((boucle-150)*5)/100);
                    listPoids.add(((boucle-150)*40)/100 + listPoids.get(5));
                    listPoids.add(((boucle-150)*20)/100 + listPoids.get(6));
                    listPoids.add(((boucle-150)*15)/100 + listPoids.get(7));
                    listPoids.add(((boucle-150)*20)/100 + listPoids.get(8));
                    
                    for (i=0;i<=boucle-150;i++)
                    {
                        listBoucleJour.add(i);
                    }
                     Collections.shuffle(listBoucleJour);
                 
             }


            /* boucle pour créer les trajets et les ajouter à la liste des trajets */
            for (i=0;i<boucle;i++)
            {
                /* Partie Date*/
          
                heure = rd.nextInt(24);
                
                //si l'on a uniquement atteint 100 voyage le matin
                if (cptMinuitHuit >= 100 && cptVingtMinuit <50)
                {
                    while (heure < 8)
                    {
                        heure = rd.nextInt(24);
                    }
                    if (heure >= 20)
                    {
                        cptVingtMinuit++;
                    }
                  
                }
                //si l'on a uniquement atteint 50 voyage le soir
                else if (cptMinuitHuit < 100 && cptVingtMinuit >=50)
                {
                    while (heure >= 20)
                    {
                        heure = rd.nextInt(24);
                    }
                    if (heure < 8)
                    {
                        cptMinuitHuit++;
                    }
                   
                }
                // si l'on a atteint 100 voyage le matin ET le soir
                else if (cptMinuitHuit >= 100 && cptVingtMinuit >=50)
                {
                    while (heure < 8 || heure >= 20)
                    {
                        heure = rd.nextInt(24);
                    }                   
                }
                // si aucun cas precedent
                else
                {
                    if (heure < 8)
                        {
                            cptMinuitHuit++;
                        }
                    if (heure >= 20)
                        {
                            cptVingtMinuit++;
                        }             
                }
                
                
                
                minute = rd.nextInt(60);
                seconde = rd.nextInt(60);

                /* Partie Etages */
                
                if (checkboxWeekEnd.isSelected())
                {
                    if (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(0)) <= 0)
                    {
                        minVal=0;
                        maxVal=3;
                        etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                    }
                    else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(0)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(1)) <= 0))
                    {
                        minVal=4;
                        maxVal=4;
                        etageFin=0;
                    }
                    else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(1)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(2)) <= 0))
                    {
                        minVal=25;
                        maxVal=39;
                        etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                    }
                    else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(2)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(3)) <= 0))
                    {
                        minVal=5;
                        maxVal=24;
                        etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                    }
                    else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(3)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(4)) <= 0))
                    {
                        minVal=40;
                        maxVal=44;
                        etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                    }
                    else
                    {
                        minVal=0;
                        maxVal=44;
                        etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                    }  
                    
                    cptSoirWK++;
                }
                else
                {

                    if (heure < 8 || heure >= 20)
                    {
                        
                        if (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(0)) <= 0)
                        {
                            minVal=0;
                            maxVal=3;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(0)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(1)) <= 0))
                        {
                            minVal=4;
                            maxVal=4;
                            etageFin=0;
                        }
                        else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(1)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(2)) <= 0))
                        {
                            minVal=25;
                            maxVal=39;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(2)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(3)) <= 0))
                        {
                            minVal=5;
                            maxVal=24;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else if ((listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(3)) > 0) && (listBoucleSoirWK.get(cptSoirWK).compareTo(listPoids.get(4)) <= 0))
                        {
                            minVal=40;
                            maxVal=44;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else
                        {
                            minVal=0;
                            maxVal=44;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }  
                        
                        cptSoirWK++;

                    }
                    else
                    {
                        if (listBoucleJour.get(cptJour).compareTo(listPoids.get(5)) <= 0)
                        {
                            minVal=0;
                            maxVal=3;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else if ((listBoucleJour.get(cptJour).compareTo(listPoids.get(5)) > 0) && (listBoucleJour.get(cptJour).compareTo(listPoids.get(6)) <= 0))
                        {
                            minVal=4;
                            maxVal=4;
                            etageFin=0;
                        }
                        else if ((listBoucleJour.get(cptJour).compareTo(listPoids.get(6)) > 0) && (listBoucleJour.get(cptJour).compareTo(listPoids.get(7)) <= 0))
                        {
                            minVal=25;
                            maxVal=39;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else if ((listBoucleJour.get(cptJour).compareTo(listPoids.get(7)) > 0) && (listBoucleJour.get(cptJour).compareTo(listPoids.get(8)) <= 0))
                        {
                            minVal=5;
                            maxVal=24;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else if ((listBoucleJour.get(cptJour).compareTo(listPoids.get(8)) > 0) && (listBoucleJour.get(cptJour).compareTo(listPoids.get(9)) <= 0))
                        {
                            minVal=40;
                            maxVal=44;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }
                        else
                        {
                            minVal=0;
                            maxVal=44;
                            etageFin=rd.nextInt(maxVal-minVal)+minVal-4;
                        }  
                        
                        cptJour++;
                    }
                    
                    
                    
                  }
                
                minVal=0;
                maxVal=44;
                
                etageDebut=rd.nextInt(maxVal-minVal)+minVal-4;
                
                if (etageDebut == etageFin)
                {
                    if (etageDebut == 40)
                    {
                        etageDebut--;
                    }
                    else
                    {
                        etageDebut++;
                    } 
                }

                
                
                
                 //System.out.println()
                
                /* Partie Nb Personnes */
                if (Integer.valueOf(texteMoyennePersonnes.getText())== 0)
                {
                     nbPersonnes = rd.nextInt(15)+1; 
                }
                else
                {
                    maxVal=Integer.valueOf(texteMoyennePersonnes.getText())+2;
                    minVal=Integer.valueOf(texteMoyennePersonnes.getText())-2;
                    
                    if (maxVal > 15)
                        maxVal=15;
                    
                    if (minVal < 0)
                        minVal = 0;
                    
                    nbPersonnes = rd.nextInt(maxVal-minVal)+minVal;
                }

                /* On élimine les trajets qui ne rentre pas dans l'intervalle de temps définit par l'utilisateur */
                if ( ((minHeure*3600)+(minMinute*60)+minSeconde) < ((heure*3600)+(minute*60)+seconde) && ((maxHeure*3600)+(maxMinute*60)+maxSeconde) > ((heure*3600)+(minute*60)+seconde))
                {
                    /* On élimine les trajets qui ne rentre pas dans l'intervalle des etages définit par l'utilisateur */
                    if ((etageDebut>=Integer.valueOf(texteLimiteEtageDebut.getText())) &&  (etageDebut<=Integer.valueOf(texteLimiteEtageFin.getText())) && (etageFin>=Integer.valueOf(texteLimiteEtageDebut.getText())) &&  (etageFin<=Integer.valueOf(texteLimiteEtageFin.getText())))
                        trajets.add(new TrajetVO(heure, minute, seconde, etageDebut, etageFin, nbPersonnes));
                }

                
            }

            
            Collections.sort(trajets);

            try 
            {
   
            /* Ouverture fichier CSV */
                
            OutputStream csv = new FileOutputStream(texteEmplacement.getText()+"\\simulation.csv");
            PrintStream l_out = new PrintStream(csv);
             
            /* Boucle pour ajouter la liste de trajet dans le ficheir CSV */
            
            for(TrajetVO tv : trajets) {
                l_out.print(String.valueOf(tv.getHeure()));
                l_out.print(":");
                l_out.print(String.valueOf(tv.getMinute()));
                l_out.print(":");
                l_out.print(String.valueOf(tv.getSeconde()));
                l_out.print(";");
                l_out.print(String.valueOf(tv.getEtageDepart()));
                l_out.print(";");
                l_out.print(String.valueOf(tv.getEtageArrivee()));
                l_out.print(";");
                l_out.print(String.valueOf(tv.getNbPersonnes()));
                l_out.print(nouvelleLigne);
            }
            
            l_out.flush(); 
            l_out.close(); 
            l_out=null;
            
            JOptionPane.showMessageDialog(null,"<html>Le fichier de simulation a été créé à l'emplacement suivant:<br>"+texteEmplacement.getText()+"</html>");
            
            this.dispose(); 
        }
        catch(FileNotFoundException exc)
        {
            
            JOptionPane.showMessageDialog(null,"<html>Chemin incorrect ou <br>Fichier destination déjà ouvert</html>");
        }
        
        
        
        
       
    }//GEN-LAST:event_boutonGenererActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(IHMFichierSimulation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(IHMFichierSimulation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(IHMFichierSimulation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(IHMFichierSimulation.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                IHMFichierSimulation ihm = new IHMFichierSimulation();
                ihm.setVisible(true);
                JFileChooser fc = new JFileChooser();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton boutonGenerer;
    private javax.swing.JButton boutonParcourir;
    private javax.swing.JCheckBox checkboxWeekEnd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel labelConsignes;
    private javax.swing.JLabel labelHeureDebut;
    private javax.swing.JLabel labelHeureFin;
    private javax.swing.JLabel labelInfo;
    private javax.swing.JLabel labelLimitesEtages;
    private javax.swing.JLabel labelTitre;
    private javax.swing.JLabel labelWeekEnd;
    private javax.swing.JTextField texteDebutHeure;
    private javax.swing.JTextField texteEmplacement;
    private javax.swing.JTextField texteFinHeure;
    private javax.swing.JTextField texteLimiteEtageDebut;
    private javax.swing.JTextField texteLimiteEtageFin;
    private javax.swing.JTextField texteMoyennePersonnes;
    // End of variables declaration//GEN-END:variables

}

